package com.zzh.oauth.config;

import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 自定义code
 */
@Component
public class CustomizeAusthorizationCodeServices implements AuthorizationCodeServices {
    private final ConcurrentMap<String, OAuth2Authentication> codes = new ConcurrentHashMap<String, OAuth2Authentication>();

    /**
     * 获取code 与 销毁 等工作
     * @param code
     * @return
     * @throws InvalidGrantException
     */
    public OAuth2Authentication consumeAuthorizationCode(String code) throws InvalidGrantException {
        return codes.remove(code);
    }
    /**
     * 创建code
     * @param oAuth2Authentication
     * @return
     */
    public String createAuthorizationCode(OAuth2Authentication oAuth2Authentication) {
        String code  = UUID.randomUUID().toString();
        codes.put(code, oAuth2Authentication);
        return code;
    }

}
